package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.Address;
import util.DBUtils;
import dao.AddressDao;

public class AddressDaoImpl implements AddressDao{

	@Override
	public List<Address> findAddress(String addr){
		List<Address> list=new ArrayList<Address>();
		//select * from areas where name like '裕华%'
		String sql = null;
		if(addr == ""){
			System.out.println(addr + "is null");
			return null;
		}
		else
			sql = "select * from areas where name like " + "'" + addr + "%" + "'";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
//			con=DBUtils.createConnection();
			con = DBUtils.createConnectionByJNDI();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			Address address = null;
			while (rs.next()) {
				String id = rs.getString("id");
				String uuid = rs.getString("uuid");
				String name = rs.getString("name");
				String code = rs.getString("code");
				address = new Address(id, uuid, name, code);
				list.add(address);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtils.close(con, pst, rs);
		}
		return list;
	}
}
